package com.zlf.sb.demo.completableFuture.bingxing;

import com.zlf.sb.demo.util.SmallTool;

import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
import java.util.stream.IntStream;

public class CompletableFutureAllOfTest {
    public static void main(String[] args) {
        SmallTool.printTimeAndThread("小白和小伙伴们 进餐厅点菜");
        // 点菜
       /* ArrayList<Dish> dishes = new ArrayList<Dish>();
        for (int i = 1; i <= 12; i++) {
            Dish dish = new Dish("菜" + i, 1);
            dishes.add(dish);
        }
        // 做菜
        ArrayList<CompletableFuture> cfList = new ArrayList<>();
        for (Dish dish : dishes) {
            CompletableFuture<Void> cf = CompletableFuture.runAsync(() -> dish.make());
            cfList.add(cf);
        }
        // 等待所有任务执行完毕
        CompletableFuture.allOf(cfList.toArray(new CompletableFuture[cfList.size()])).join();
        SmallTool.printTimeAndThread("小白和小伙伴们 进餐厅点菜");
        CompletableFuture[] dishes = IntStream.rangeClosed(1, 12)
                .mapToObj(i -> new Dish("菜" + i, 1))
                .map(dish -> CompletableFuture.runAsync(dish::make))
                .toArray(size -> new CompletableFuture[size]);

        CompletableFuture.allOf(dishes).join();*/
    }
}
